package com.free.semantic.common;

import com.free.semantic.core.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContextImpl implements Context {

    private final RelationItem relationItem;

    public ContextImpl(RelationItem relationItem) {
        this.relationItem = relationItem;
    }

    Map<String, ParamNode> valueMap = new HashMap<>();

    @Override
    public <T> void setValue(String object, T value) {
        if (!relationItem.containObj(object)) {
            throw new RuntimeException(String.format("设置对象%s失败", object));
        }
        valueMap.put(object, new ParamNode(object, value));
    }

    @Override
    public <T> List<T> getListValue(String object) {
        if (!relationItem.containObj(object)) {
            throw new RuntimeException(String.format("获取对象%s失败", object));
        }
        if (!valueMap.containsKey(object)) {
            throw new RuntimeException("context设置异常");
        }
        return valueMap.get(object).value();
    }

    @Override
    public <T> void setValue(RangeObj object, T value) {
        setValue(object.getObjectName(), value);
    }

    @Override
    public <T> List<T> getListValue(RangeObj object) {
        return getListValue(object.getObjectName());
    }

}
